package lsy.Dao;

import lsy.entity.Admin;
import lsy.tools.PropertiesUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class AdminDao {

    public void add(Admin admin) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "insert into admin(userName,pwd,name) values(?,?,?)";
        queryRunner.update(sql,admin.getUserName(),admin.getPwd(),admin.getName());
    }
    public void delete(Integer uid) throws SQLException{
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "delete from admin where uid=?";
        queryRunner.update(sql,uid);
    }
    public void update(Admin admin) throws SQLException{
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "update admin set uerName=?,pwd=?,name =? where uid=?";
        queryRunner.update(sql,admin.getUserName(),admin.getPwd(),admin.getName(),admin.getUid());
    }
    public List<Admin> list(Admin admin) throws SQLException{
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from admin";
        List<Admin> list =queryRunner.query(sql,new BeanListHandler<>(Admin.class));
        return list;
    }
    public Admin findById(Integer uid) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from admin where uid=?";
        Admin admin =queryRunner.query(sql,new BeanHandler<>(Admin.class),uid);
        return admin;
    }
    public Admin login(String userName,String pwd) throws SQLException {
        QueryRunner queryRunner= new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from admin where userName=? and pwd=?";
        Admin entity =queryRunner.query(sql,new BeanHandler<>(Admin.class),userName,pwd);
        return entity;
    }
}
